package com.byzx.wine.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.byzx.wine.po.Franchisee;
import com.byzx.wine.service.IFranchiseeService;
import com.byzx.wine.util.Page;

@RequestMapping("/franChisee")
@Controller
public class FranChiseeAction {
	@Autowired
	private IFranchiseeService franchiseeService;
	

	@RequestMapping("/index")
	public String login(HttpServletResponse response, HttpServletRequest request) {
		return "franchisee/FranChisee";
	}
	
	@RequestMapping("searchFranchisee")
	@ResponseBody
	public Map<String, Object> searchWine(Model model, Integer iDisplayLength,
			Integer sEcho, Integer iDisplayStart) {
		if (sEcho == null) {
			sEcho = 1;
		}
		if (iDisplayLength == null) {
			iDisplayLength = 10;
		}
		if (iDisplayStart == null) {
			iDisplayStart = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(
				iDisplayStart, iDisplayLength, map);
		Page<Map<String, Object>> page1 = franchiseeService.findPage(page);

		map.put("sEcho", sEcho);
		map.put("iTotalRecords", page1.getTotalCount());
		map.put("iTotalDisplayRecords", page1.getTotalCount());
		map.put("aaData", page1.getList());
		return map;
	}
	
	@RequestMapping("addFranchisee")
	@ResponseBody
	public Map<String,Object> addFranchisee(Franchisee record){
		Map<String, Object> map = new HashMap<String, Object>();
		Integer i = franchiseeService.insert(record);
		map.put("i", i);
		return map;
	}
	
	
	@RequestMapping("updateFranchisee")
	@ResponseBody
	public Map<String,Object> selectFranchisee(Integer id){
		Map<String, Object> map = new HashMap<String, Object>();
		Franchisee record = franchiseeService.selectByPrimaryKey(id);
		map.put("record", record);
		return map;
	}
	
	@RequestMapping("updateData")
	@ResponseBody
	public Map<String,Object> updateByPrimaryKey(Franchisee record){
		Map<String, Object> map = new HashMap<String, Object>();
		Integer i = franchiseeService.updateByPrimaryKey(record);
		map.put("i", i);
		return map;
	}
	
	
	@RequestMapping("deleteOnce")
	@ResponseBody
	public Map<String,Object> deleteByPrimaryKey(Integer id){
		Map<String, Object> map = new HashMap<String, Object>();
		Integer i = franchiseeService.deleteByPrimaryKey(id);
		map.put("i", i);
		return map;
	}
	
	@RequestMapping("deleteAll")
	@ResponseBody
	public Map<String,Object> deleteAll(String ids){
		Map<String, Object> map = new HashMap<String, Object>();
			String [] id = ids.split(",");
			for(int i = 0;i < id.length;i++){
				int n = Integer.parseInt(id[i]);
				Integer s = franchiseeService.deleteByPrimaryKey(n);
				map.put("s", s);
			}
			return map;
	}
	
}
